package com.xdk.templatescala.utils;

import org.slf4j.MDC;

/**
 * ThreadContext
 * @author jhz
 **/
public class ThreadContext {

    /**
     * 日志中唯一值key
     */
    private static final String REQUEST_HEADER_KEY_LOG_REQ_ID = "requestId";

    /**
     * 当前业务Id
     */
    private static final String REQUEST_HEADER_KEY_PROJECT_ID = "projectid";

    /**
     * 当前业务对应的用户Id
     */
    private static final String REQUEST_HEADER_KEY_USER_ID = "userid";

    /**
     * 设置公共参数LOG_REQ_ID_KEY
     */
    public static void setReqId(String id) {
        MDC.put(REQUEST_HEADER_KEY_LOG_REQ_ID, id);
    }

    public static String getReqId() {
        return MDC.get(REQUEST_HEADER_KEY_LOG_REQ_ID);
    }

    /**
     * 设置公共参数businessId
     */
    public static void setProjectId(String projectId) {
        MDC.put(REQUEST_HEADER_KEY_USER_ID, projectId);
    }

    public static Long getProjectId() {
        return Long.parseLong(MDC.get(REQUEST_HEADER_KEY_PROJECT_ID));
    }

    /**
     * 设置公共参数currentUserId
     */
    public static void setUserId(String businessUserId) {
        MDC.put(REQUEST_HEADER_KEY_USER_ID, businessUserId);
    }

    public static Long getUserId() {
        return Long.parseLong(MDC.get(REQUEST_HEADER_KEY_USER_ID));
    }

    /**
     * 清除参数
     */
    public static void clearMdc() {
        MDC.clear();
    }
}
